package com.neiquan.backstage.resourec_manage.webaction;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Paging;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.pub.bsc.dao.po.AdminUser;
import com.neiquan.backstage.pub.bsc.dao.po.Station;
import com.neiquan.backstage.resourec_manage.ResourecStationService;

/**
 * 工位操作
 * @author linjiang
 *
 */
@Controller
@RequestMapping(value = "resourecStation", produces = { "text/json;charset=UTF-8" })
public class ResourecStationAction {
	

	private static Class<?> poc = Station.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private ResourecStationService resourecStationService;
	
	/**
	 * 	添加Station集合
	 */
	@RequestMapping(value={"addStation"})
	public String addStation(@RequestParam("info")String[] infos,Integer lines,Integer rows,double rental,Integer state,HttpSession session){
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		for(String info:infos){
			Station entityS = new Station();
			String[] split = info.split(",");
			String name=split[0];
			String myRows1=split[1];
			String myLines1=split[2];
			int myRows = Integer.parseInt(myRows1);
			int myLines=Integer.parseInt(myLines1);
			entityS.setName(name);
			entityS.setRental(rental);
			entityS.setTotalLines(lines);
			entityS.setTotalRows(rows);;
			entityS.setMyLines(myLines);
			entityS.setMyRows(myRows);
			entityS.setMemberId(memberId);
			entityS.setState(0);
			resourecStationService.add(entityS);
		}
		return "redirect:../../resourec_station_list.jsp";
	}
/**
 * 	分页查询
 */
	@ResponseBody
	@RequestMapping(value={"list"})
	public String list(int pageIndex, int pageSize,HttpSession session){
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Station> paging = (List<Station>)extraSpringHibernateTemplate.getHibernateTemplate().find("From Station where memberId=?", memberId);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	} 
	
/**
 * 	删除
 */
	@ResponseBody
	@RequestMapping(value={"remove"})
	public String remove(int id){
		 Station entity = resourecStationService.queryById(id);
		if (entity == null) {
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		extraSpringHibernateTemplate.getHibernateTemplate().delete(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
/**
 * 	all删除
 */
	@ResponseBody
	@RequestMapping(value={"removeAll"})
	public String removeAll(@RequestParam(value = "items[]") int[] items){
		for(int a:items){
			Station entity = resourecStationService.queryById(a);
			if (entity == null) {
				return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
			}
			extraSpringHibernateTemplate.getHibernateTemplate().delete(entity);
		}
		
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
/**
 * 	冻结
 */
	@ResponseBody
	@RequestMapping(value={"congelation"})
	public String congelation(int id){
		Station entity = resourecStationService.queryById(id);
		if(entity == null){
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		entity.setState(2);
		resourecStationService.updata(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
/**
 * all冻结
 * @param items
 * @return
 */
	@ResponseBody
	@RequestMapping(value={"congelationAll"})
	public String congelationAll(@RequestParam(value = "items[]") int[] items){
		for(int a:items){
			Station entity = resourecStationService.queryById(a);
			if (entity == null) {
				return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
			}
			entity.setState(2);
			resourecStationService.updata(entity);
		}
		
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
/**
 * 	updAllRental批量修改租金
 */
	@ResponseBody
	@RequestMapping(value={"updAllRental"})
	public String updAllRental(@RequestParam(value = "items[]") int[] items,double rental){
		for(int a:items){
			Station entity = resourecStationService.queryById(a);
			if (entity == null) {
				return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
			}
			entity.setRental(rental);
			resourecStationService.updata(entity);
		}
		
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
/**
 * 	通过Id查询station
 */
	@ResponseBody
	@RequestMapping(value={"queryById"})
	public String queryById(int id){
		Station entity = resourecStationService.queryById(id);
		if(entity == null){
			return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
		}
		return CommonResp.buildSuccessResp(entity).toJsonString();
	} 
	/**
	 * 	通过Id查询station
	 */
		@ResponseBody
		@RequestMapping(value={"queryByStationIds"})
		public String queryByStationIds(String momentStationIds){
			List<Station> listS = new ArrayList<>();
			String[] split = momentStationIds.split(",");
			for(String str:split){
				int id = Integer.parseInt(str);
				Station entityS = resourecStationService.queryById(id);
				listS.add(entityS);
			}
			
			return CommonResp.buildSuccessResp(listS).toJsonString();
		} 
/**
 * 	修改Station
 */
	@RequestMapping(value={"edit"})
	public String edit(int id,String name,double rental){
		Station entity = resourecStationService.queryById(id);
		if(entity == null){
			return "redirect:../../resourec_station_list.jsp?statusShow=1&showMsg=该工位不存在";
		}
		entity.setName(name);
//		entity.setZone(zone);
		Integer state1=entity.getState();
		if(state1!=0){
			entity.setRental(rental);
			resourecStationService.updata(entity);
			return "redirect:../../resourec_station_list.jsp?statusShow=1&showMsg=该工位已被占用或被出租不能修改租金";
		}
		resourecStationService.updata(entity);
		return "redirect:../../resourec_station_list.jsp";
	}
	
	/**
	 * 	修改Station
	 */
		@ResponseBody
		@RequestMapping(value={"eidtnName"})
		public String eidtnName(int id,String name){
			Station entity = resourecStationService.queryById(id);
			if(entity == null){
				return CommonResp.buildErrorResp(BackendErrorCode.not_exist).toJsonString();
			}
			entity.setName(name);
			resourecStationService.updata(entity);
			
			return CommonResp.buildSuccessResp(entity).toJsonString();
		}
	/**
	 * 	检查location
	 */
/*	@ResponseBody
	@RequestMapping(value={"checkLocation"})
	public String checkLocation(Integer id,Integer rows,String lines, double rental,HttpSession session) {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Criterion> criterions = new ArrayList<>();
		criterions.add(Restrictions.eq("memberId", memberId));
		criterions.add(Restrictions.eq("zone", zone));
		criterions.add(Restrictions.ne("stationId",id));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.build(poc, criterion);
		 List<Station> list = extraSpringHibernateTemplate.findAllByCriteria(queryCountCriteria);
		String str=null;
		for(Station s:list){
			if(location.equals("")){
				str="true";
				return CommonResp.buildSuccessResp(str).toJsonString();
			}
		}
		
		return CommonResp.buildSuccessResp().toJsonString();
	}*/
	
/**
 * 查询所有queryAll zone group by去重
 */
/*	@ResponseBody
	@RequestMapping(value={"queryAll"})
	public String queryAll(HttpSession session) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = DetachedCriteria.forClass(poc);
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		dc.add(Restrictions.eq("memberId",memberId));
		dc.setProjection(Projections.alias(Projections.groupProperty("zone"),"zone"));
		 List<Station> list = extraSpringHibernateTemplate.findAllByCriteria(dc);
		return CommonResp.buildSuccessResp(list).toJsonString();
	}*/
	
/**
 * 	条件查询
 */
	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String name, HttpSession session) throws ParseException {
		AdminUser loginedUser = (AdminUser) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(name)) {
			criterions.add(Restrictions.like("name", "%" + name + "%"));
		}
		/*if (StringUtils.isNoneBlank(zone)) {
			criterions.add(Restrictions.eq("zone", zone));
		}*/
		criterions.add(Restrictions.eq("memberId", memberId));
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("stationId"),
				criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0,
				Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}
	
}
